using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Workflows.Components.FileManipulation.Properties;

namespace Workflows.Components.FileManipulation
{
    [Serializable]
    public class CompressionComponent:WorkflowComponentBase
    {
        #region props

        private CompressionInstruction _Instruction;
        private CompressionOutput _Output;

        public string SrcPath
        {
            get { return this._Instruction.SrcPath; }
            set { this._Instruction.SrcPath = value; }
        }

        public string TgtPath
        {
            get { return this._Instruction.TgtPath; }
            set { this._Instruction.TgtPath = value; }
        }

        public CompressionActionType Action
        {
            get { return this._Instruction.Action; }
            set { this._Instruction.Action = value; }
        }

        public override InstructionBase Instruction
        {
            get { return this._Instruction; }
            set { this._Instruction = (CompressionInstruction) value;}
        }

        public override ResultBase Output
        {
            get { return this._Output; }
            set { this._Output=(CompressionOutput)value; }
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        public CompressionComponent():base()
        {
            this.ComponentActivityType = WorkflowActivityType.SimpleActivity;
            this.ComponentCategory = WorkflowComponentCategory.File_Tasks.ToString();
            this.Description = "Zip/Unzip a file or folder";
            this.ComponentIcon = Resources.zip2;
            this.ComponentImage = Resources.tska;
            this.ComponentName = "Compress/Decompress task";

            this._Instruction =
                new CompressionInstruction(this.ComponentID, Guid.Empty, string.Empty, string.Empty,
                                           CompressionActionType.WinzipCompress);
            this._Output=new CompressionOutput(this.ComponentID,Guid.Empty,string.Empty);
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SourcePath", this.SrcPath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TargetPath", this.TgtPath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "CompressionAction", ((int)this.Action).ToString());

            return xNode;
        }

        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                CompressionComponent compressionComponent = base.Instantiate(xNode) as CompressionComponent;

                compressionComponent.SrcPath = xNode.Attributes.GetNamedItem("SourcePath").Value;
                compressionComponent.TgtPath = xNode.Attributes.GetNamedItem("TargetPath").Value;
                compressionComponent.Action =
                    (CompressionActionType) int.Parse(xNode.Attributes.GetNamedItem("CompressionAction").Value);
                return compressionComponent;
            }
            else
            {
                throw new Exception("Invalid type to instantiate");
            }
        }
    }
}
